package com.hex.liyue.usermodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.usermodule.adapter.AdapterAddress;
import com.hex.liyue.usermodule.bean.AddressResultBean;
import com.hex.liyue.usermodule.presenter.AddressPresenter;
import com.hex.liyue.usermodule.presenter.IAddressView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 这是一个我的地址界面
 * Created by WJ on 2016/6/15.
 */
public class AddressActivity extends BaseActivity implements IAddressView, AdapterAddress.OnItemClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_address_list)
    RecyclerView rvAddressList;
    @Bind(R.id.empty_address)
    LinearLayout emptyAddress;

    private AddressPresenter addressPresenter;
    private AddressResultBean addressResultBean;

    @Override
    protected void bindPresenter() {
        addressPresenter = new AddressPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        addressPresenter.requestAddressList();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_my_address;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("地址管理");
    }

    @OnClick({R.id.iv_toobar_back, R.id.tv_add_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_add_address:
                startAddAddressActivity();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == 1 || requestCode == 2) && resultCode == RESULT_OK) {
            addressPresenter.requestAddressList();
        }
    }

    @Override
    public void startAddAddressActivity() {
        Intent intent = new Intent(this, EditAddressActivity.class);
        intent.putExtra("type", 2);
        startActivityForResult(intent, 2);
    }

    @Override
    public void setAdapter(AddressResultBean addressResultBean) {
        L.d("WJ", "==" + addressResultBean.data);
        if (addressResultBean.data != null && addressResultBean.data.size() > 0) {
            this.addressResultBean = addressResultBean;
            AdapterAddress adapterAddress = new AdapterAddress();
            adapterAddress.setList(addressResultBean.data);
            adapterAddress.setOnItemClickListener(this);
            rvAddressList.setAdapter(adapterAddress);
            rvAddressList.setLayoutManager(new LinearLayoutManager(this));
            rvAddressList.setVisibility(View.VISIBLE);
            emptyAddress.setVisibility(View.GONE);
        } else {
            rvAddressList.setVisibility(View.GONE);
            emptyAddress.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void onEditClickListener(int position) {
        Intent intent = new Intent(this, EditAddressActivity.class);
        intent.putExtra("type", 1);
        intent.putExtra("dataBean", addressResultBean.data.get(position));
        startActivityForResult(intent, 1);
    }

    @Override
    public void onDeleteClickListener(int position) {
        addressPresenter.requestDelete(addressResultBean.data.get(position).delivery_id);
    }

    @Override
    public void onFirstClickListener(int position) {
        addressPresenter.requetsUpdateFirst(addressResultBean.data.get(position).delivery_id);
    }

    @Override
    public void onItemClickListener(int position) {
        boolean confiremReceiptRun = getIntent().getBooleanExtra("flag", false);
        L.d("WJ", "是不是收货地址进来的==" + confiremReceiptRun);
        if (confiremReceiptRun) {
            //选了一个地址  返回去吧
            setResult(RESULT_OK);
            //更新一下
            UserManager.updateAddress(addressResultBean.data.get(position));
            finish();
        }
    }

}
